//
//  LoginUpView.swift
//  DollBuy
//
//  Created by 郑东喜 on 2016/11/17.
//  Copyright © 2016年 郑东喜. All rights reserved.
//  登陆页面上班部分uiview自定义

import UIKit

//代理
protocol LoginUpViewDelegate {
    ///注册单机事件
    func rigSEL()
    
    ///注册忘记密码事件
    func forSEL()
    
    ///登陆事件
    func logSE()
    
    ///接收登陆成功返回值
    func logSuc()
    
}


class LoginUpView: UIView,UITextFieldDelegate {
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //底部label
        self.bottomLabel()
        
        //中间视图
        self.cView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //中间视图
    var centerView = UIView()
    
    //设置代理监听按钮事件
    var delegate : LoginUpViewDelegate?
    
    //当前全局变量
     var tfNum = UITextField()
    
     var tfPass = UITextField()
}

// MARK:- 中间视图
extension LoginUpView {
    
    
    func cView() -> Void {
        
        //中间视图宽度
        let centerViewW = (SW - 30)

        centerView = UIView(frame: CGRect(x: 15, y: centerViewW * 0.3 , width: centerViewW, height: centerViewW * 0.55))
//        centerView.backgroundColor = UIColor.white
        self.addSubview(centerView)
        
        //添加中间视图控件
        addControls()
    }
    
    //添加中间的控件
    func addControls() -> Void {
        
        //电话号码前置图片
        let personImg = UIImageView(frame: CGRect(x: 5, y: 5, width: 20, height: 20))
//        personImg.backgroundColor = UIColor.black
        personImg.image = UIImage.init(named: "user")
        centerView.addSubview(personImg)


        //电话号码
        let tfNum = TfPlaceHolder.init(frame: CGRect(x: 35, y: 0, width: centerView.frame.size.width - 35, height: 30))
        
        //设置代理
        tfNum.delegate = self

        tfNum.plStrSize(str: "请输入您的手机号", holderColor: UIColor.gray, textFontSize: 13)

        //全局变量赋值
        self.tfNum = tfNum
        centerView.addSubview(tfNum)
        
        //分割线
        let separateLine = UIView(frame: CGRect(x: 0, y: tfNum.frame.size.height + 2, width: centerView.frame.size.width, height: 1))
        
        //设置为灰色
        separateLine.backgroundColor = UIColor.darkGray
        centerView.addSubview(separateLine)
        

        //密码(账号的高度加上三倍的margin)
        let tfPass = TfPlaceHolder.init(frame: CGRect(x: 35, y: 30 + 10, width: centerView.frame.size.width - 35, height: 30))
        
        //设置代理
        tfPass.delegate = self

        tfPass.plStrSize(str: "请输入您的密码", holderColor: .gray, textFontSize: 13)
        
        //全局变量赋值
        self.tfPass = tfPass
        centerView.addSubview(tfPass)
        
        
        //忘记密码前置图片
        let passImg = UIImageView(frame: CGRect(x: 5, y: 30 + 15, width: 20, height: 20))
//        passImg.backgroundColor = UIColor.black
        
        passImg.image = UIImage.init(named: "pass")
        centerView.addSubview(passImg)
        
        //分割线(上面高度相加在加上分割线距离控件的间距)
        let separateLineTwo = UIView(frame: CGRect(x: 0, y: tfNum.frame.size.height + tfPass.frame.size.height + 15, width: centerView.frame.size.width, height: 1))
        
        //设置为灰色
        separateLineTwo.backgroundColor = UIColor.darkGray
        centerView.addSubview(separateLineTwo)
        
        //登陆按钮
        let loginBtn = UIButton(frame: CGRect(x: 0, y: tfNum.frame.size.height + tfPass.frame.size.height + 25, width: centerView.frame.size.width, height: 40))
        
        loginBtn.backgroundColor = UIColor.orange
        loginBtn.layer.cornerRadius = 10
        
        
        //添加登陆事件
        
        loginBtn.addTarget(self, action: #selector(LoginUpView.logSEL), for: .touchUpInside)
        loginBtn.setTitle("登录", for: .normal)
        centerView.addSubview(loginBtn)
        
        //手机号注册
        let rW = 30 * 2 + 5 * 3 + 2 * 2 + 40 + 10 + 5
        let rigiLabel = UILabel(frame: CGRect(x: 0, y: rW, width: 80, height: 10))
        rigiLabel.font = UIFont.systemFont(ofSize: 11)
        rigiLabel.text = "手机号注册"
        rigiLabel.textColor = UIColor.orange
        centerView.addSubview(rigiLabel)
        
        
        
        //忘记密码
        let forLabel = UILabel(frame: CGRect(x: Int(centerView.frame.size.width - 80)
            , y: rW, width: 80, height: 10))
        forLabel.font = UIFont.systemFont(ofSize: 11)
        forLabel.text = "忘记密码?"
        forLabel.textAlignment = NSTextAlignment.right
        centerView.addSubview(forLabel)
        
        
        
        //注册label添加手势
        //单机手势，使其可以跳转页面
        let riglabelTapGes = UITapGestureRecognizer.init(target: self, action: #selector(LoginUpView.rigisterClickLabel))
        
        //添加手势交互
        rigiLabel.isUserInteractionEnabled = true
        
        rigiLabel.addGestureRecognizer(riglabelTapGes)
        
        //忘记密码label添加手势
        //单机手势，使其可以跳转页面
        let forlabelTapGes = UITapGestureRecognizer.init(target: self, action: #selector(LoginUpView.forClickLabel))
        
        //添加手势交互
        forLabel.isUserInteractionEnabled = true
        
        forLabel.addGestureRecognizer(forlabelTapGes)
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.endEditing(true)
        return true
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.endEditing(true)
    }

}

// MARK:- label手势事件
extension LoginUpView {
    //注册label事件单机响应
    func rigisterClickLabel() -> Void {
        print("手机号注册")
        delegate?.rigSEL()
        
    }
    
    //忘记密码label事件单机响应
    func forClickLabel() -> Void {
//        print("忘记密码")
        delegate?.forSEL()
        //跳转页面

    }
}

// MARK:- 登陆事件
extension LoginUpView {
    func logSEL() -> Void {
        print("上班登陆")
        delegate?.logSE()
        
        self.endEditing(true)
        
        //模型传值
        LoginModel.shared.login(tfNum: self.tfNum, tfPass: self.tfPass) { (loginStatus) in
            print("登陆返回",loginStatus)
//            MBManager.showBriefMessage("登陆中...", in: self)
            if loginStatus == "恭喜您登录成功" {
                
                //
                let mainVC = MainViewController()
                
                mainVC.mvc = MyVC()
                
               UIApplication.shared.keyWindow?.rootViewController = mainVC
                
                self.delegate?.logSuc()
//                _meREfersh = 1
                
            }
        }
        
    }
}

// MARK:- 底部label位置
extension LoginUpView {
    //底部label位置
   fileprivate func bottomLabel() -> Void {
    
        //左边的线
        let leftView = UIView(frame: CGRect(x: 15, y: self.frame.size.height - 10, width: self.frame.size.width * 0.3 - 5, height: 1))
        leftView.backgroundColor = UIColor.gray
        self.addSubview(leftView)
        
        //右边的线d
        let rightView = UIView(frame: CGRect(x: self.frame.size.width * 0.625 + 15, y: self.frame.size.height - 10, width: self.frame.size.width * 0.3 - 5, height: 1))
        rightView.backgroundColor = UIColor.gray
        self.addSubview(rightView)

        let bottLabel = UILabel.init(frame: CGRect(x: 0.0, y: self.frame.size.height - 20, width: self.frame.size.width, height: 2 * 10))
        bottLabel.text = "使用第三方登陆"
        bottLabel.textAlignment = NSTextAlignment.center
        bottLabel.font = UIFont.systemFont(ofSize: 12)
        
        self.addSubview(bottLabel)
        
    }
}



